package io.gitee.cisumer.amazing;

import io.gitee.cisumer.amazing.master.Ground;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 大神们的玩具，原文地址:<br/>
 * https://codegolf.stackexchange.com/questions/35569/tweetable-mathematical-art<br/>
 * 原版为C++，改为Java，通过Pixel的子类实现的三原色算法，经由每个像素点的颜色色值最终渲染出一个1024*1024的图片
 * @author gitee.com/cisumer
 * @date create in 2022/3/17
 */
public class Picture {
	private Pixel pixel;
	private Picture(){}
	public static Picture build(Pixel pixel){
		Picture image=new Picture();
		image.pixel=pixel;
		return image;
	}
	public String title(){
		return "运算："+pixel.name();
	}
	public Image render(){
		BufferedImage image = new BufferedImage(Pixel.SIZE, Pixel.SIZE, BufferedImage.TYPE_INT_RGB);
		for(int i=0;i<Pixel.SIZE;i++){
			for(int j=0;j<Pixel.SIZE;j++){
				int color=pixel.rgb(i,j );
//				System.out.printf("i:%d,j:%d = %s\r\n",i,j,Integer.toHexString(color));
				image.setRGB(i, j, color);
			}
		}
		return image;
	}
	
	public static void main(String[] args){
		JFrame frame=new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(Pixel.SIZE, Pixel.SIZE);
		frame.setVisible(true);
		Picture pic= Picture.build(new Ground());
		frame.setTitle(pic.title());
		frame.getGraphics().drawImage(pic.render(), 0, 0, null);
	}
}
